package org.one.stone.soup.grfxML;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;

import org.one.stone.soup.mjdb.data.field.BoolField;
import org.one.stone.soup.mjdb.data.field.FloatField;
import org.one.stone.soup.mjdb.data.field.IntField;
import org.one.stone.soup.mjdb.data.field.Point2DField;
import org.one.stone.soup.xml.XmlElement;
  /*
 * Copyright 2000-2001 Wet-Wired.com Ltd.,
 * Portsmouth England
 * All rights reserved.
 */

/**
	*
	* @author Nik Cross
*/
public class GeometryLine extends Geometry{

	DataPoint windowField;
	DataPoint boundsField;

	BoolField visibleField;

	FloatField transparencyField;
	boolean transparent;

	DataColor colorField;
	Color color;

	Point2DField positionField;
	Point2DField offsetField;

	Point2DField startField;
	int startX;
	int startY;

	Point2DField endField;
	int endX;
	int endY;

	IntField thicknessField;
	BasicStroke thickness;

public GeometryLine(
		GrfxMLEngine engine,
		DataPoint windowF,
		DataPoint boundsF,
		BoolField visibleF,
		FloatField transparencyF,
		DataColor colorF,
		BoolField filledF,
		Point2DField positionF,
		Point2DField offsetF,
		Point2DField startF,
		Point2DField endF,
		IntField thicknessF
	) {
	super(engine);

	windowField = windowF;
	boundsField = boundsF;
	visibleField = visibleF;
	transparencyField = transparencyF;
	colorField = colorF;
	positionField = positionF;
	offsetField = offsetF;
	startField = startF;
	endField = endF;
	thicknessField = thicknessF;

	process(true);
}
/**
 * contains method comment.
 */
public boolean contains(Point2DField point,Point2DField pointView) {
	return false;
}
public void draw(Graphics grfx)
{
//	process(false);

	if(visibleField.getValue()==false || transparencyField.getValue()==1)
		return;
	if(visibleField.hasChanged())
		process(true);
	else
		process(false);

	if(
		//Both x's < 0
		(startX<0 && endX<0)
		||
		//Both y's <0
		(startY<0 && endY<0)
		||
		//Both x's too big
		(startX>engine.BASE.SETTINGS.frameSize.getValueX() && endX>engine.BASE.SETTINGS.frameSize.getValueX())
		||
		//Both y's too big
		(startY>engine.BASE.SETTINGS.frameSize.getValueY() && endY>engine.BASE.SETTINGS.frameSize.getValueY())
	)
	return;

	grfx.setClip(
		windowField.getValueXMin(),
		windowField.getValueYMin(),
		windowField.getValueX(),
		windowField.getValueY()
	);
	grfx.setColor(color);

	Stroke stroke = ((Graphics2D)grfx).getStroke();
	((Graphics2D)grfx).setStroke( thickness );

	grfx.drawLine(startX,startY,endX,endY);

	((Graphics2D)grfx).setStroke( stroke );
}
private void process(boolean forced)
{
	if(forced==true)
	{
		color = GrfxMLProcessor.getColorFor(colorField);

		startX = startField.getValueX();
		startY = startField.getValueY();
/*		startX += positionField.getValueX();
		startY += positionField.getValueY();*/
		startX += offsetField.getValueX();
		startY += offsetField.getValueY();

		endX = endField.getValueX();
		endY = endField.getValueY();
/*		endX += positionField.getValueX();
		endY += positionField.getValueY();*/
		endX += offsetField.getValueX();
		endY += offsetField.getValueY();

		boundsField.setValueXMin(startX);
		boundsField.setValueYMin(startY);

		boundsField.setValueXMax(endX);
		boundsField.setValueYMax(endY);

		boundsField.update();

		if(thicknessField.getValue()<1)
		{
			thickness = new BasicStroke( 1 );
		}
		else
		{
			thickness = new BasicStroke( thicknessField.getValue() );
		}
	}
	else
	{
		if(colorField.hasChanged())
		{
			color = GrfxMLProcessor.getColorFor(colorField);
		}

		if(thicknessField.hasChanged())
		{
			if(thicknessField.getValue()<1)
			{
				thickness = new BasicStroke( 1 );
			}
			else
			{
				thickness = new BasicStroke( thicknessField.getValue() );
			}
		}

		if(startField.hasChanged() || positionField.hasChanged() || offsetField.hasChanged())
		{
			startX = startField.getValueX();
			startY = startField.getValueY();
/*			startX += positionField.getValueX();
			startY += positionField.getValueY();*/
			startX += offsetField.getValueX();
			startY += offsetField.getValueY();

			boundsField.setValueXMin(startX);
			boundsField.setValueYMin(startY);
			boundsField.update();
		}

		if(endField.hasChanged() || positionField.hasChanged() || offsetField.hasChanged())
		{
			endX = endField.getValueX();
			endY = endField.getValueY();
/*			endX += positionField.getValueX();
			endY += positionField.getValueY();*/
			endX += offsetField.getValueX();
			endY += offsetField.getValueY();

			boundsField.setValueXMax(endX);
			boundsField.setValueYMax(endY);
			boundsField.update();
		}
	}
}
/**
 *
 * @return java.lang.String
 */
public XmlElement toXML() {
	XmlElement element = new XmlElement("Line");

	if(getId()!=null)
		element.addAttribute("id",getId());

	element.addAttribute("visible",""+visibleField.getValue());
	element.addAttribute("transparency",""+transparencyField.getValue());

	element.addAttribute("x",""+startX);
	element.addAttribute("y",""+startY);

	element.addAttribute("red",""+colorField.getValueX());
	element.addAttribute("green",""+colorField.getValueY());
	element.addAttribute("blue",""+colorField.getValueZ());

	element.addAttribute("endX",""+endX);
	element.addAttribute("endY",""+endY);

	XmlElement bounds = element.addChild("Clip");
	bounds.addAttribute( "x",""+windowField.getValueXMin() );
	bounds.addAttribute( "y",""+windowField.getValueYMin() );
	bounds.addAttribute( "width",""+windowField.getValueX() );
	bounds.addAttribute( "height",""+windowField.getValueY() );

	return element;
}

public boolean hasChanged()
{
	if(
	        windowField.hasChanged()==true ||
	        boundsField.hasChanged()==true ||
	        visibleField.hasChanged()==true ||
	        transparencyField.hasChanged()==true ||
	        colorField.hasChanged()==true ||
	        positionField.hasChanged()==true ||
	        offsetField.hasChanged()==true ||
	        positionField.hasChanged()==true ||
	        offsetField.hasChanged()==true ||
	    	startField.hasChanged()==true ||
	    	endField.hasChanged()==true ||
	    	thicknessField.hasChanged()==true
	)
	{
	    return true;
	}
	
	return false;
}

public void resetChanged()
{
    windowField.resetChanged();
    boundsField.resetChanged();
    visibleField.resetChanged();
    transparencyField.resetChanged();
    colorField.resetChanged();
    positionField.resetChanged();
    offsetField.resetChanged();
    positionField.resetChanged();
    offsetField.resetChanged();
    startField.resetChanged();
	endField.resetChanged();
	thicknessField.resetChanged();
}
}
